package com.dba.example.gwt.client.commands.impl;

import javax.inject.Inject;

import com.dba.example.gwt.client.beans.factories.CompanyAutoBeanFactory;
import com.dba.example.gwt.client.beans.pages.CompanyPage;
import com.dba.example.gwt.client.commands.CompanyPageCallback;
import com.dba.example.gwt.client.commands.RequestBuilderCommand;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Command;
import com.google.inject.assistedinject.Assisted;
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;

public class CompanyPageCommand extends RequestBuilderCommand implements Command  {
	
	private CompanyAutoBeanFactory factory;
	
	private int offset;
	
	private int size;
	
	private String sortColumn;
	
	private String sortDirection;
	
	private String search;
	
	private CompanyPageCallback callback;
	
	@Inject
	public CompanyPageCommand(CompanyAutoBeanFactory factory, @Assisted("offset") int offset, @Assisted("size") int size, @Assisted("sortColumn") String sortColumn, @Assisted("sortDirection") String sortDirection, @Assisted("search") String search, @Assisted CompanyPageCallback callback) {
		this.factory = factory;
		this.offset = offset;
		this.size = size;
		this.sortColumn = sortColumn;
		this.sortDirection = sortDirection;
		this.search = search;
		this.callback = callback;
	}

	@Override
	public void execute() {
		getRequest("rest/company/page?offset=" + offset + "&size=" + size + "&sortColumn=" + sortColumn + "&sortDirection=" + sortDirection + "&search=" + URL.encode(search));
	}
	
	public void success(String json) {
		AutoBean<CompanyPage> bean = AutoBeanCodex.decode(factory, CompanyPage.class, json);
		callback.success(bean.as());
	}

	public void error(Request request, Throwable exception) {
		callback.error(exception.getMessage());
	}

}
